package com.utils.commons;

import org.springframework.data.domain.Page;

import com.alibaba.fastjson.JSON;

/**
 * @author geliyang
 *
 * @param <T>
 */
public class ResponseBean<T> {
	
	private int status;
	
	private String text;
	
	private T data;
	
	public ResponseBean(T data, int status, String text) {
		this.status = status;
		this.data = data;
		this.text = text;
	}

	@SuppressWarnings("unchecked")
	public static<T> String of(T data, int status, String text) {
		if(data == null) {
			return JSON.toJSONString(new ResponseBean<T>((T) "", status, text));
		}
		if(data instanceof org.springframework.data.domain.Page) {
			return JSON.toJSONString(new ResponseBean<T>((T)PageUtils.of((Page<?>) data), status, text));
		}
		return JSON.toJSONString(new ResponseBean<T>(data, status, text));
	}
	
	public static<T> String of(T data, int status) {
		return ResponseBean.of(data, status, null);
	}
	
	public static<T> String of(int status) {
		return ResponseBean.of(null, status, null);
	}
	
	public static<T> String of(int status, String text) {
		return ResponseBean.of(null, status, text);
	}
	
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getText() {
		if(this.text == null) {
			switch (status) {
			case 0:
				return "失败";
			case 1:
				return "成功";
			default:
				return "";
			}
		}
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	
}
